# -*- mode: python; -*-

#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#
import os

Import('CfgmEnv')
env = CfgmEnv.Clone()

setup_sources = [
    'setup.py',
]

setup_sources_rules = []
for file in setup_sources:
    setup_sources_rules.append(
        env.Install(Dir('.'), "#controller/src/container/mesos-cni/" + file))

local_sources = [
    'mesos_cni/__init__.py',
    'mesos_cni/mesos_cni.py',
    'mesos_cni/tests/__init__.py',
]

local_sources_rules = []
for file in local_sources:
    target = "/".join(file.split('/')[:-1])
    local_sources_rules.append(
        env.Install(target, "#controller/src/container/mesos-cni/" + file))

cd_cmd = 'cd ' + Dir('.').path + ' && '
sdist_depends = []
sdist_depends.extend(setup_sources_rules)
sdist_depends.extend(local_sources_rules)
sdist_gen = env.Command('dist/mesos_cni-0.1dev.tar.gz', 'setup.py',
                        cd_cmd + 'python setup.py sdist')

env.Depends(sdist_gen, sdist_depends)
env.Default(sdist_gen)

if 'install' in BUILD_TARGETS:
    install_cmd = env.Command(None, 'setup.py',
                              cd_cmd + 'python setup.py install %s' %
                              env['PYTHON_INSTALL_OPT'])
    env.Depends(install_cmd, sdist_depends)
    env.Alias('install', install_cmd)

env.Alias('install', env.Install(env['INSTALL_CONF'],
    'contrail-mesos-cni.conf'))

buildspace_link = os.environ.get('CONTRAIL_REPO')
if buildspace_link:
    # in CI environment shebang limit exceeds for python
    # in easy_install/pip, reach to it via symlink
    top_path = buildspace_link + '/' + Dir('.').path
    top_dir = Dir(top_path)
else:
    top_dir = Dir('.')
